package View;

import java.util.ArrayList;
import java.util.Scanner;

import Controller.ProductController;
import Controller.UserController;
import Helper.Helper;
import Model.ProductM;
import Model.User;
import Model.Product;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;

public class UserIndex {
    public static void index(String username){
        Helper.clear();
        System.out.printf("*-----[ 欢迎,%5s ]-----*\n\n", username);

        System.out.println("\t 1. 浏览商品");
        System.out.println("\t 2. 搜索商品");
        System.out.println("\t 3. 查看已购买商品\n");

        System.out.println("\t 4. 注销");

        System.out.println("*---------------------------*");
    }

    public static void listProducts(){
        System.out.println("[ 所有商品 ]\n");

        System.out.println("编号 |   名称   |   价格  | 数量");
        ArrayList<Product> products = ProductController.listProducts();

        for( Product item : products ){
            if( item.amount > 0 ){
                System.out.printf("%4d  %6s  %2.1f  %4d\n", item.id ,item.name, item.price, item.amount);
            }
        }

        buy();
    }

    public static void searchProduct(){
        System.out.println("[ 搜索商品 ]\n");
        System.out.print("请输入商品名: ");

        Scanner scanner = new Scanner(System.in);
        String productName = scanner.next();
        while( productName.matches("[`~!@#$%^&*()+=|{}':;,\\[\\].<>/?！￥…（）—]+") ){
            System.err.println("商品名不合法！");
            System.out.print("请重新输入商品名： ");

            productName = scanner.next();
        }

        System.out.println("编号 |   名称   |   价格  | 数量");
        ArrayList<Product> products = ProductController.searchProduct(productName);

        for( Product item : products ){
            if( item.amount > 0 ){
                System.out.printf("%4d  %6s  %2.1f  %4d\n", item.id ,item.name, item.price, item.amount);
            }
        }

        buy();
    }

    public static void listBuyRecord(){
        System.out.println("[ 已购买的商品 ]\n");

        System.out.println("编号 |   名称   |   价格   | 数量");
        ArrayList<Product> products = UserController.listBuyRecord();

        for( Product item : products ){
            System.out.printf("%4d  %6s  %2.1f %4d\n", item.id ,item.name, item.price, item.amount);
        }

        Helper.waitter();
    }

    private static void buy(){
        if( Helper.brancher("是否购买？", false) ){
            Scanner scanner = new Scanner(System.in);

            int id,amount;
            String temp;
            do{
                System.out.print("请输入商品编号: ");
                temp = scanner.next();
            }while( !temp.matches("[0-9]+") || Integer.parseInt(temp)<=0 );
            id = Integer.parseInt(temp);

            ProductM productM = new ProductM();
            ArrayList<Product> p = productM.find(id);

            if( p.size() == 0 ){
                System.err.println("不存在编号为"+id+"的商品！");
                Helper.waitter();
                return;
            }

            System.out.println("正在购买： 【 "+p.get(0).name+" 】");

            do{
                System.out.print("请输入购买数量: ");
                temp = scanner.next();
            }while( !temp.matches("[0-9]+") || Integer.parseInt(temp)<=0 );
            amount = Integer.parseInt(temp);

            if( !Helper.brancher("确定？", true) ){
                return;
            }

            if( UserController.buy(id, amount) ){
                System.out.println("购买成功！");
            }else{
                System.err.println("购买失败！");
            }

            Helper.waitter();
        }
    }
}
